package com.core.gig.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.core.as.ASException;
import com.core.gig.as.ASConfiguracao;
import com.core.gig.as.ASFactory;
import com.core.gig.dto.EncargoDTO;
import com.core.gig.util.GIGUtil;

/**
 * @author Lucas Ceni
 *
 */
public class EncargoAction
		extends GIGAction
{
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return inicial(mapping, form, request, response);
	}

	public ActionForward inicial(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		clearForm(form);
		
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			EncargoDTO dto = as.recuperarEncargo();
			
			request.setAttribute("encargo", dto);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		
		return mapping.findForward(FWD_DEFAULT);
	}
	
	public ActionForward salvar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		EncargoDTO dto = new EncargoDTO();
		GIGUtil.copyProperties(dto, form);
		
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			as.salvarEncargo(dto);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		
		addMessage(request, "global.msg.sucess.save", ICO_OK);
		return inicial(mapping, form, request, response);
	}
}